<?php
/**
 * V6
 *
 */
class data_invitation_insite extends data {
	public function __construct() {
		parent::__construct();
		$this->expire_days = 6;			// public code的过期时间，天数
	}
	/**
	 * 列出给我发送过来的邀请
	 *
	 * @param int $uid
	 * @return array
	 */
	public function ls ($uid) {
		$uid = intval($uid);
		
		if ($uid) {
			$execute = $this->db->execute('
				select
					invitation_insite.id			AS id,
					invitation_insite.inviter_id	AS inviter_id,
					""								AS inviter_nickname,
					invitation_insite.dt			AS dt,
					circle.id						AS circle_id,
					circle.title					AS circle_title,
					circle.privacy					AS circle_privacy
				from
					invitation_insite, circle
				where
					invitation_insite.accepter_id		= "'.$uid.'"					and
					invitation_insite.is_accepted		= 0								and
					circle.id							= invitation_insite.circle_id
				order by 
					invitation_insite.id DESC
			');
			if (is_array($execute)) {
				$data_user = new data_user();
				foreach ($execute as $k=>$v) {
					$execute[$k]['inviter_nickname'] = $data_user->friend_to_nickname($uid, $v['inviter_id']);
				}
				return $execute;
			} else return false;
		} else return false;
	}
	/**
	 * 列出可以发送邀请的好友列表
	 * 		排除已经是成员的好友
	 * 		排除已经发送过insite邀请的好友
	 *
	 * @param int $uid				// 发送邀请者的id
	 * @param int $circle_id		// 圈子id
	 * @return array = array (
	 * 		array (id, user_id, friend_id, nickname), ...
	 * )
	 */
	public function friend_for_invite ($uid, $circle_id) {
		$uid 		= intval($uid);
		$circle_id 	= intval($circle_id);
		
		if ($uid and $circle_id) {
			$execute = $this->db->execute('
				select
					*
				from
					friend
				where
					user_id 		= "'.$uid.'"		and
					friend_id 		NOT IN (
						select
							user_id
						from
							circle_member
						where
							circle_id = "'.$circle_id.'"	and
							(type = "member" or type = "admin" or type = "founder")
					) and
					friend_id		NOT IN (
						select
							accepter_id
						from
							invitation_insite
						where
							inviter_id	= "'.$uid.'"	and
							circle_id	= "'.$circle_id.'"
					)
				order by id ASC
			');
			if ($execute) {
				$data_user = new data_user();
				foreach ($execute as $k=>$v) {
					$execute[$k]['friend_nickname'] = $data_user->friend_to_nickname($uid, $v['friend_id']);
				}
				return $execute;
			} else return false;
		} else return false;
	}
	public function add ($uid, $circle_id, $friends_id_str) {
		$uid				= intval($uid);
		$circle_id			= intval($circle_id);
		$friends_id_array	= format_id_string($friends_id_str, true);		// 返回 string
		
		if ($uid and $circle_id and $friends_id_array) {
			foreach ($friends_id_array as $fid) {
				$sql .= '("'.$uid.'", "'.$circle_id.'", "'.$fid.'", "'.date('Y-m-d H:i:s').'", 0),';
			}
			$sql = substr($sql, 0, -1);		// 去掉最后一个“,”
			return $this->db->execute('
				insert ignore into
					invitation_insite
				(inviter_id, circle_id, accepter_id, dt, is_accepted)
					values
				'.$sql.'
			');
		} else return false;
	}
	/**
	 * 接受圈子邀请
	 * 		添加圈子成员
	 * 		更新circle invite表的状态
	 *
	 * @param int $uid
	 * @param int $id
	 * @return int
	 */
	public function accept ($uid, $id) {
		$uid	= intval($uid);
		$id		= intval($id);
		
		if ($uid and $id) {
			// 读取最新的一条邀请信息
			$invitation = $this->db->execute('
				select
					*
				from
					invitation_insite
				where
					id				= "'.$id.'"				and
					accepter_id		= "'.$uid.'"
			');
			if ($invitation) {
				$data_circle = new data_circle();
				$add_member = $data_circle->add_member($invitation[0]['inviter_id'], $invitation[0]['accepter_id'], $invitation[0]['circle_id']);		// 已经检查了邀请者的权限
				
				if ($add_member) {
					// 更新所有该圈子的邀请为1
					$this->update_is_accepted($invitation[0]['accepter_id'], $invitation[0]['circle_id'], 1);
					
					return true;
				} else {
					// 不成功删除该条
					$this->del($id);
					return false;
				}
			} else return false;
		} else return false;
	}
	private function update_is_accepted ($accepter_id, $circle_id, $is_accepted=1) {
		$accepter_id	= intval($accepter_id);
		$circle_id		= intval($circle_id);
		$is_accepted	= intval($is_accepted);
		
		if ($accepter_id and $circle_id) {
			return $this->db->execute('
				update
					invitation_insite
				set
					is_accepted = "'.$is_accepted.'"
				where
					accepter_id	= "'.$accepter_id.'"	and
					circle_id	= "'.$circle_id.'"
			');
		} else return false;
	}
	private function del ($id) {
		$id		= intval($id);
		if ($id) {
			return $this->db->execute('
				delete from
					invitation_insite
				where
					id		= "'.$id.'"
			');
		} else return false;
	}
	/**
	 * 拒绝邀请，更新circle invite状态为-1
	 *
	 * @param int $uid
	 * @param int $id
	 * @return int
	 */
	public function refuse ($uid, $id) {
		$uid	= intval($uid);
		$id		= intval($id);
		
		if ($uid and $id) {
			$execute = $this->db->execute('
				select 
					* 
				from 
					invitation_insite 
				where 
					id 				= "'.$id.'" 	and 
					accepter_id 	= "'.$uid.'"
			');
			if ($execute) {
				return $this->update_is_accepted($execute[0]['accepter_id'], $execute[0]['circle_id'], -1);
			} else return false;
		} else return false;
	}
	public function ignore ($uid, $id) {
		$uid	= intval($uid);
		$id		= intval($id);
		
		if ($uid and $id) {
			return $this->db->execute('
				delete from
					invitation_insite
				where
					id				= "'.$id.'"			and
					accepter_id		= "'.$uid.'"
			');
		} else return false;
	}
	/**
	 * 用户是否被邀请
	 *
	 * @param int $inviter_id
	 * @param int $accepter_id
	 * @param int $circle_id
	 * @return Array
	 */
	public function is_invited ($inviter_id, $accepter_id, $circle_id) {
		$inviter_id		= intval($inviter_id);
		$accepter_id	= intval($accepter_id);
		$circle_id		= intval($circle_id);
		
		if ($inviter_id and $accepter_id and $circle_id) {
			$execute = $this->db->execute('
				select 
					*
				from
					invitation_insite
				where
					inviter_id		= "'.$inviter_id.'"		and
					accepter_id		= "'.$accepter_id.'"	and
					circle_id		= "'.$circle_id.'"
				limit
					1
			');
			if ($execute) {
				return $execute[0];
			} else return false;
		} else return false;
	}
	/**
	 * 清理掉所有过期的邀请
	 *
	 * @param int $expire_days
	 * @return bool
	 */
	public function clear_expire_code ($expire_days=30) {
		$expire_days = intval($expire_days);
		
		if ($expire_days) {
			$this->db->execute('
				delete from
					invitation_insite
				where
					dt	< "'.date('Y-m-d H:i:s', (time() - ($expire_days * 86400))).'"
			');
			return true;
		} else return false;
	}

}
?>